﻿var _mainVue = new Vue({
    el: '#_mainApp',
    data: function () {

        return {
            defaultStyle: {
                height: ""
            },
            isCollapse: false,
            fullscreen: false,
            tabConentStyle: {
                height: "600px",
                width: "100%"
            },
            tabConentIframeStyle: {
                height: "600px",
                width: "100%"
            },
            defaultActiveId: "",
            activeid: "0",
            menuTabs: [],
            headerTabs: [],
            contextMenu: {
                visible: false,
                isDisabledCloseLeftBtnFlag: true,
                isDisabledCloseRightBtnFlag: true,
                menuContainerStyle: { left: "", top: "" }
            },

            modifypassworddialogVisible: false,
            modifyForm: {
                password: "",
                newPassword: "",
                comfirmNewPassword: ""
            },
            modifyFormRules: {
                password: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
                newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
                comfirmNewPassword: [{ required: true, message: '请输入确认密码', trigger: 'blur' }]
            },
        }
    },
    created: function () {

        this.initStyle();
        this.addHomeTab();
        this.getMemnus();


    },
    methods: {

        initStyle: function () {
            var self = this;

            self.getHeight();
            self.getTabContentHeight();
            window.addEventListener("resize", function () {
                self.getHeight();
                self.getTabContentHeight();
            });
            window.addEventListener("click", function () {
                self.contextMenu.visible = false;
            });
        },
        getMemnus: function () {
            var _this = this;
            _this.$get("../home/getmenu", {}).then((response) => {
                _this.menuTabs = response;
                if (_this.menuTabs != null && _this.menuTabs.length > 0) {
                    _this.defaultActiveId = _this.menuTabs[0].children[0].id;
                }
            }).catch((error) => {
            });
        },

        toggleSideBar: function () {
            this.isCollapse = !this.isCollapse
        },
        loginOut: function () {
            this.$confirm('确认退出?', '提示', {})
                .then(() => {
                    delCookie('authuser');
                    window.location.href = "../account/login";
                })
                .catch(() => { });
        },
        //定义方法，获取高度减去头尾
        getHeight: function () {
            this.defaultStyle.height = window.innerHeight + "px";
        },
        getTabContentHeight: function () {
            this.tabConentStyle.height = (window.innerHeight - 60) + "px";
            this.tabConentIframeStyle.height = (window.innerHeight - 60 - 52) + "px";
        },
        changeCollapse: function () { // 更改左侧导航栏展示状态
            this.isCollapse = !this.isCollapse;
        },

        closeAllTabs: function () {
            let tabs = this.headerTabs.filter(tab => tab.closable == false);
            if (tabs == null || tabs.length == 0) return;
            let nextTab = tabs[0];
            if (nextTab) this.activeid = nextTab.id;
            this.headerTabs = tabs;
            this.closeContextMenu();
        },

        closeOtherTabs(type) {
            var _activeid = this.activeid;
            if (type == "left") {
                var newTabs = [];
                var canAdd = false;
                for (let item of this.headerTabs) {
                    if (item.closable == false) {
                        newTabs.push(item);
                        continue;
                    }
                    if (item.id == _activeid) canAdd = true;

                    if (canAdd == true) {
                        newTabs.push(item);
                    }
                }

                this.headerTabs = newTabs;

            } else if (type == "right") {
                var newTabs = [];
                var canAdd = true;
                for (let item of this.headerTabs) {

                    if (item.closable == false) {
                        newTabs.push(item);
                        continue;
                    }
                    if (canAdd == true) {
                        newTabs.push(item);
                    }
                    if (item.id == _activeid) canAdd = false;

                }

                this.headerTabs = newTabs;
            } else if (type == "other") {
                this.headerTabs = this.headerTabs.filter(tab => tab.id == _activeid || tab.closable == false);
            }

            this.closeContextMenu();
        },
        addHomeTab: function () {
            this.addTab({
                id: "0",
                name: "首页",
                path: "../dashboard/index"
            }, false);
        },
        addTab: function (item, closable) {
            var id = item.id, name = item.name, path = item.path, closable = closable == undefined ? true : closable;
            if (item.path == "") return;

            let flag = false;
            let self = this;
            for (let tab of self.headerTabs) {
                if (tab.id === id) {
                    self.activeid = id;
                    flag = true;
                    return false;
                }
            }
            if (!flag) {
                self.headerTabs.push({
                    id: id,
                    name: name,
                    closable: closable,
                    path: path,
                    sourceid: item.sourceid,
                    iframeid: "iframe_tab_" + id
                });
                self.activeid = id;
            }
        },
        removeTab(targetid) {
            let _menuTabs = this.menuTabs;
            let self = this;
            let _activeid = self.activeid;
            self.headerTabs.forEach((tab, index) => {
                if (tab.id == targetid) {
                    let nextTab = self.headerTabs[index + 1] || self.headerTabs[index - 1];
                    if (nextTab) _activeid = nextTab.id;
                }
            });
            self.activeid = _activeid;
            self.headerTabs = self.headerTabs.filter(tab => tab.id !== targetid);
        },
        /*
         *关闭当前页签,优先激活前一个页签
         **/
        closeCurrentTab: function () {
            let _menuTabs = this.menuTabs;
            let self = this;
            let _activeid = self.activeid;
            let _sourceid = "";
            var currentTabs = self.headerTabs.filter(tab => tab.id == self.activeid);
            if (currentTabs != null) {
                var currentTab = currentTabs[0];
                var sourceTabs = self.headerTabs.filter(tab => tab.id == currentTab.sourceid);
                if (sourceTabs != null && sourceTabs.length > 0) {
                    self.headerTabs = self.headerTabs.filter(tab => tab.id !== self.activeid);
                    self.activeid = currentTab.sourceid;
                    return;
                }
            }
            self.headerTabs.forEach((tab, index) => {
                if (tab.id == _activeid) {
                    let preTab = self.headerTabs[index - 1] || self.headerTabs[index + 1];
                    if (preTab) _activeid = preTab.id;
                }
            });
            self.headerTabs = self.headerTabs.filter(tab => tab.id !== self.activeid);
            self.activeid = _activeid;
        },
        //刷新Tab
        curTabReload: function () {
            this.reload()
        },
        closeContextMenu: function () {
            this.contextMenu.visible = false;
        },

        openContextMenu: function (e) {
            e.preventDefault();
            let obj = e.srcElement ? e.srcElement : e.target;

            if (obj.id) {
                let currentContextTabId = obj.id.split("-")[1];
                let curIndex = 0;
                for (; curIndex < this.headerTabs.length; ++curIndex) {
                    if (this.headerTabs[curIndex].id == currentContextTabId) {
                        break;
                    }
                }
                if (this.headerTabs[curIndex].closable == false) {
                    return;
                }

                this.activeid = this.headerTabs[curIndex].id;

                if (curIndex <= 0) {
                    this.contextMenu.isDisabledCloseLeftBtnFlag = true
                    this.contextMenu.isDisabledCloseRightBtnFlag = false
                } else if (curIndex >= this.headerTabs.length - 1) {
                    this.contextMenu.isDisabledCloseLeftBtnFlag = false
                    this.contextMenu.isDisabledCloseRightBtnFlag = true
                } else {
                    this.contextMenu.isDisabledCloseLeftBtnFlag = false
                    this.contextMenu.isDisabledCloseRightBtnFlag = false
                }
                this.contextMenu.visible = true;
                this.contextMenu.menuContainerStyle.left = (e.clientX) + "px";
                this.contextMenu.menuContainerStyle.top = (e.clientY + 10) + "px";
            }
        },
        logout: function () {
            this.$confirm('确认退出?', '提示', {})
                .then(() => {
                    sessionStorage.removeItem('user');
                    this.$router.push('/login');
                })
                .catch(() => { });
        },
        getDefaultHeaderStyle: function () {
            this.defaultHeaderStyle.width = (window.innerWidth - 200) + "px";
            $("#mainContainer").width(this.defaultHeaderStyle.width);
        },
        //刷新Tab
        refreshTab: function () {
            this.reload()
        },
        // 全屏事件
        handleFullScreen: function () {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        },
        openModifyPassword: function () {
            var _this = this;
            _this.modifypassworddialogVisible = true;
        },
        modifyPassword: function () {
            var _this = this;

            _this.$refs.modifyFormRules.validate((valid) => {
                if (!valid) {
                    return false;
                }
                _this.$post("../User/ModifyPassword", _this.modifyForm).then((res) => {
                    if (res.success == true) {
                        _this.$success(res.message, function () {
                            _this.modifypassworddialogVisible = false;
                        });
                    } else {
                        _this.$failed(res.message);
                    }

                }).catch((error) => {
                });

            })

        }
    },
});





/**
 * 
 * @param {any} id 表单单据唯一id 
 * @param {any} name 页签名称
 * @param {any} path 页签路径
 */
function addTabToMain(id, name, path) {

    _mainVue.addTab({
        id: _mainVue.activeid + "_" + id,
        name: name,
        path: path,
        sourceid: _mainVue.activeid
    });
}


function backToPre(refresh) {
    _mainVue.closeCurrentTab();

    if (refresh == undefined || refresh == false) return;

    var frameid = "iframe_tab_" + _mainVue.activeid;
    var iframe = window.frames[frameid];

    if (iframe.contentWindow) {
        if (typeof (iframe.contentWindow.refreshFromChild) == "function") {
            //For Chrome
            iframe.contentWindow.refreshFromChild();
        }
    }
    else if (iframe.window) {
        if (typeof (iframe.window.refreshFromChild) == "function") {
            //For IE
            iframe.window.refreshFromChild();
        }
    }

}


function errortop(message, callback) {
    _mainVue.$message.error({
        message: message,
        duration: 1000,
        onClose: () => {
            if (callback)
                callback();
        }
    });
}